package com.spring.hotel.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spring.hotel.common.JsonResponse;
import com.spring.hotel.entity.*;
import com.spring.hotel.mapper.CheckinMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CheckinService extends ServiceImpl<CheckinMapper, Checkin> {

    @Autowired
    private CheckinMapper checkinMapper;

    //分页
    public JsonResponse pageList(Integer id, PageDTO pageDTO) {

        List<CheckinRoomDTO> dataList = checkinMapper.getList(id);
        Page<CheckinRoomDTO> page = new Page<>(pageDTO.getPageNum(), pageDTO.getPageSize());
        page.setRecords(dataList);
        page.setTotal(dataList.size());
        return JsonResponse.success(page);
    }

    //状态
    public JsonResponse gerState(Integer userid) {
        CheckinStateDTO checkinStateDTO = checkinMapper.getState(userid);
        if (checkinStateDTO== null) {
            checkinStateDTO=new CheckinStateDTO();
            checkinStateDTO.setState(0);
        }
        return JsonResponse.success(checkinStateDTO);
    }

    //更新
    public JsonResponse updateByUser(String userid){
        String str=String.valueOf(LocalDateTime.now());
        checkinMapper.updateByUserId(userid,str);
        return JsonResponse.success(null);
    }

    //删除
    public JsonResponse delete(Checkin checkin){
        checkinMapper.delete(checkin.getUserid());
        return JsonResponse.success(null);
    }

    //展示退房记录
    public JsonResponse pageList(Checkin checkin, PageDTO pageDTO) {
        QueryWrapper<Checkin> queryWrapper = new QueryWrapper<>();
        if (checkin.getId() != null) {
            queryWrapper.lambda().like(Checkin::getId, checkin.getId());
        }
        Page<Checkin> page = this.page(new Page<>(pageDTO.getPageNum(), pageDTO.getPageSize()), queryWrapper);
        return JsonResponse.success(page);
    }
}
